package com.quickcancellation.crm.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: 自定义异常处理
 * @author: mxs
 * @date: 2023年2月5日22:50:02
 * @version: v1.0
 */
@ControllerAdvice
public class MyExceptionHandler {

    private final static Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);

    /**
     * 未登录
     * @param e
     * @return
     */
    @ExceptionHandler(value = NotLoginException.class)
    @ResponseBody
    public ResponseDto NotLoginExceptionHandler(NotLoginException e){
        //System.out.println("全局异常捕获>>>:"+e);

        logger.error("NotLoginException异常",e);

        return ResponseUtil.fail("-1001","请登录");
    }

    /**
     * 没有权限异常
     * @param e
     */
    @ExceptionHandler(value = NotRoleException.class)
    @ResponseBody
    public ResponseDto NotRoleExceptionException(Exception e) {
        logger.error("NotRoleException异常",e);
        return ResponseUtil.fail("-1002","您无权限操作");
    }

    /**
     * 兜底执行的
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseDto finalHandler(Exception e){
        logger.error("Exception异常",e);

        return ResponseUtil.fail("-10001","出错啦，请连线管理员");
    }
}
